<?php
namespace Web\Controller;
use Think\Controller;

class BlockController extends BaseController {
    public function index(){
       
        $modules = M('tpl_module')
        ->alias('m')
        ->join('LEFT JOIN '.C('DB_PREFIX').'tpl_module_template tp ON m.id =tp.module_id')
        ->join('LEFT JOIN '.C('DB_PREFIX').'tpl_module_template_param tpp ON m.id =tpp.module_id')
        ->field("m.*,count(tpp.id) as tpp_count, count(DISTINCT(tp.id)) as tp_count")
        ->group("m.id")
        ->select();
        //echo  M('tpl_module')->getLastSql();
        
        $this->assign("modules",$modules); 
        $this->display();
    }
    
    public function template($module_id){
        $where['module_id'] = $module_id;
        $module_template = M('tpl_module_template')->where($where)->select();
        $this->assign("module_template",$module_template); 
        $this->assign("module_id",$module_id); 
        $this->display();
    }
    
    public function template_detail($id){
        $where['id']  = $id;
        $template = M('tpl_module_template')->where($where)->find();
        //根据cpn类型和分组类型取得点位
        $map['cpn_type']  =  $template['cpn_type'];
        $map['group_code']  =  $template['group_code'];
        $list =  M('device_class_param')
        ->alias('m')
        ->join('LEFT JOIN '.C('DB_PREFIX').'tpl_module_template_param tmt ON tmt.device_param_id = m.id and tmt.template_id =  '.$id)
        ->join('LEFT JOIN '.C('DB_PREFIX').'device_sensor cc ON cc.sensor_code=m.sensor_type')
        ->join('LEFT JOIN '.C('DB_PREFIX').'device_control dc ON dc.control_code=m.control_type')
        ->where($map)->order("m.id")->field("m.*,dc.sorts as control_sorts, cc.is_count,cc.sensor_name,dc.control_name,tmt.id as tpl_param_id,tmt.is_select")->select(); 
        $show_list = array();
        $old_control_type = "";
        $old_sensor_type = "";
        $index = 0;
        $is_save = 0;
        foreach($list as $k =>$v){
        
            //if($v['control_type'] != ""){ 
            //   $key = $v['control_type']."_".$v['control_type_group'];
            //   $v['group'] = $v['control_type_group'];
             //  $v['type'] = "control_type";
            //   $show_list[$key]['list'][] = $v;
            //   $show_list[$key]['name'] = $v['control_name'];
             //  $show_list[$key]['group'] = $v['control_type_group'];
             //  $show_list[$key]['count'] = count($show_list[$key]['list']);
                
            //} else{
               $key = $v['sensor_type']."_".$v['sensor_type_group'];
               $v['group'] = $v['sensor_type_group'];
               $v['type'] = "sensor_type";
               $show_list[$key]['list'][] = $v; 
               $show_list[$key]['name'] = $v['sensor_name'];
               $show_list[$key]['control_sorts'] = $v['control_sorts'];
               $show_list[$key]['count'] = count($show_list[$key]['list']);
               if($v['tpl_param_id'] >0){
                   $is_save = 1;
               }
               
           // }
            
        } 
        $this->assign("template",$template); 
        $this->assign("show_list",$show_list);
        $this->assign("is_save",$is_save);
        $this->assign("id",$id);  
        $this->display();
    }
    
    public function set_template_detail($module_id,$template_id){
         $data = I('data/a');
         $id  = I("id");
         $add_list = array();
         foreach($data as $k =>$v){
             if($v['param_show'] == "on"){
                 $temp = array();
                 $temp['module_id'] = $module_id;
                 $temp['template_id'] = $template_id;
                 $temp['device_param_id'] = $k;
                 $temp['create_user'] = session(C('SESSION_ADMIN_ID'));
                 $temp['create_time'] = date('Y-m-d H:i:s');
                 $temp['is_select'] = 0;
                 if($v['param_select'] == "on"){
                    $temp['is_select'] = 1;    
                 }
                 $add_list[] = $temp;
             
             }
             
         }
         //先删除原来得 
         $map['module_id'] = $module_id;
         $map['template_id'] = $template_id ;
         
         $mod = M('tpl_module_template_param');
         $mod->where($map)->delete();
         $mod->addAll($add_list);
          $this->success('保存成功', 'template/module_id/'.$module_id);
        
        
    }
    
    public function editTemplate($module_id,$id=0){
       
        if(IS_POST){
            $act = I('act', 'add');
            $dec = I('dec', '');
            $module_id = I('module_id', '0');  
            $rules = array(
                array('template_name','require','模板名称必填！'),
                array('cpn_type','require','单元类型必填！'),
                array('group_code','require','专用设备控制器类型必填！'),
           );
            
            $Project = M('tpl_module_template');
            if (!$Project->validate($rules)->create()){
                 exit('<script>parent.notify("error","提示","' . $Project->getError() . '");</script>');
            }else{
                $Project->update_time = date('Y-m-d H:i:s');
                $Project->dec =  $dec;
                if($act == 'add'){
                    //判断是否包括此类型模板
                    $count_where['module_id'] = $module_id;
                    $count_where['cpn_type']   = $Project->cpn_type;
                    $count_where['group_code'] = $Project->group_code;
                    $count = $Project->where($count_where)->count();
                    if($count >0) {
                        echo '<script>parent.notify("error","提示","此类型模板已存在！");</script>';
                        exit;
                    }

                    $Project->create_user = session(C('SESSION_ADMIN_ID'));
                    $Project->created_at = date('Y-m-d H:i:s');
                    $ins = $Project->add();
                    writeLog(session(C('SESSION_ADMIN_ID')), 1, '创建模版', '', 0);
                } else {
                    $ins = $Project->save();
                    writeLog(session(C('SESSION_ADMIN_ID')), 1, '修改模版', '', 0);
                }
            }

            if($ins !== false){
                echo '<script>parent.notify("success","提示","项目保存成功！");parent.location="' . U('template',array('module_id'=>$module_id)) . '";</script>';
            } else {
                echo '<script>parent.notify("error","提示","项目保存失败！");</script>';
            }
        }
        else {
            $map['id'] = $id;
            $vo = M('tpl_module_template')->where($map)->find($id);
            $class_list = M('cpn_class')->where("is_show= 1")->select();
            $this->assign("cpn_class",$class_list);
            $this->assign('data', $vo);
            $this->assign('module_id',$module_id);
            $this->display();
        }
    }
    
     public function delTemplate($id=0){
        $template_name = M('tpl_module_template')->where(array('id'=>$id))->getField('template_name');
        M('tpl_module_template')->where(array('id'=>$id))->delete();
        M('tpl_module_template_param')->where(array('template_id'=>$id))->delete();
        writeLog(session(C('SESSION_ADMIN_ID')), 2, '删除'.$template_name, '', $id);
        echo '1';
    }
    
     public function editBlock($id=0){
        if(IS_POST){
            $act = I('act', 'add');
            $dec = I('dec', '');
            $rules = array(
                array('name','require','模块名称必填！'),
           );
            
            $Project = M('tpl_module');
            if (!$Project->validate($rules)->create()){
                 exit('<script>parent.notify("error","提示","' . $Project->getError() . '");</script>');
            }else{
                $Project->update_time = date('Y-m-d H:i:s');
                $Project->dec =  $dec;
                if($act == 'add'){
                    $Project->create_user = session(C('SESSION_ADMIN_ID'));
                    $Project->created_at = date('Y-m-d H:i:s');
                    $ins = $Project->add();
                    writeLog(session(C('SESSION_ADMIN_ID')), 1, '创建模块', '', 0);
                } else {
                    $ins = $Project->save();
                    writeLog(session(C('SESSION_ADMIN_ID')), 1,'修改模块', '', 0);
                }
            }

            if($ins !== false){
                echo '<script>parent.notify("success","提示","项目保存成功！");parent.location="' . U('index') . '";</script>';
            } else {
                echo '<script>parent.notify("error","提示","项目保存失败！");</script>';
            }
        } else {
            $map['id'] = $id;
            
            $vo = M('tpl_module')->where($map)->find($id);

            $this->assign('data', $vo);
            $this->display();
        }
    }
    
    public function delBlock($id=0){
        $project_name = M('tpl_module')->where(array('id'=>$id))->getField('name');
        M('tpl_module')->delete($id);
        M('tpl_module_template')->where(array('module_id'=>$id))->delete();
        M('tpl_module_template_param')->where(array('module_id'=>$id))->delete();
        writeLog(session(C('SESSION_ADMIN_ID')), 1, '删除'.$project_name.'项目', '', 0);
        echo '1';
    }

              
}